<template>
  <div class="account-view">
    <div class="top">账号设置</div>
    <div class="bottom">
      <div class="password">
        <span>密码</span>
        <el-button type="text" @click="dialogVisible = true">重置</el-button>
      </div>
    </div>
    <el-dialog
      title="重置密码"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="新密码" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input
            type="password"
            v-model="form.checkPassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserId } from "../../../utils/auth";
import { updatePassword } from "../../../api/personal";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        userId: getUserId(),
        password: "",
      },
      rules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码长度不能少于6个字符", trigger: "blur" },
        ],
        checkPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请再次输入密码"));
              } else if (value !== this.form.password) {
                callback(new Error("两次输入密码不一致!"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 取消
    clear() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.form.password = "";
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          updatePassword(this.form.userId, this.form.password).then((res) => {
            if (res.data.code === 1) {
              this.$notify({
                title: "成功",
                message: "密码修改成功",
                type: "success",
              });
              this.clear();
            } else {
              this.$notify.error({
                title: "错误",
                message: "网络错误",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      // 可以在这里添加关闭对话框前的验证逻辑
      done();
    },
  },
};
</script>

<style lang="less" scoped>
.account-view {
  .top {
    font-size: 20px;
    font-weight: 700;
    padding-bottom: 15px;
    border-bottom: 1px solid #e4e6eb;
  }
  .bottom {
    .password {
      border-bottom: 1px solid #e4e6eb;
      padding: 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>